package io.zt.dangyuan;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import io.zt.dangyuan.object.ReadLoveModel;
import io.zt.dangyuan.util.Cons;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 2015-248-pc on 15/7/9.
 */
public class ReadLoveAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private List<ReadLoveModel.ReadLoveObject> datas;
    private Context context;
//    private BitmapUtils bitmapUtils;

    public ReadLoveAdapter(Context context,List<ReadLoveModel.ReadLoveObject> datas) {
        super();
        this.context   =context;
        this.mInflater = LayoutInflater.from(context);
        this.datas     = datas;

//        bitmapUtils = new BitmapUtils(context);
//        bitmapUtils.configMemoryCacheEnabled(true);
//        bitmapUtils.configDefaultLoadingImage(R.drawable.icon_default);
//        bitmapUtils.configDefaultLoadFailedImage(R.drawable.icon_default);

    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder=new ViewHolder();
            convertView = mInflater.inflate(R.layout.read_love_item, null);
            holder.headImage = (ImageView) convertView.findViewById(R.id.read_love_item_image);
            holder.nameText = (TextView) convertView.findViewById(R.id.read_love_item_name);
            holder.phoneText = (TextView) convertView.findViewById(R.id.read_love_item_phone);
            holder.jobText = (TextView) convertView.findViewById(R.id.read_love_item_job);
            holder.sexImage  = (ImageView) convertView.findViewById(R.id.read_love_item_sex);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }



        ReadLoveModel.ReadLoveObject object  = datas.get(position);

        holder.jobText.setText(object.getWorkunit());

        GlobalDataApplication app = (GlobalDataApplication) ((BaseActivity) context).getApplication();
        if (app.getUserModel()!=null) {
            holder.nameText.setText(object.getRealname());
            holder.phoneText.setText(object.getPhonenum());
        }else {
            String  name = object.getRealname();
            if (name!=null)
            {
                if (name.length() >0)
                {
                    String name2=  name.substring(0,1);

                    name  = name2 + "**";
                }


            }

            holder.nameText.setText(name);
            holder.phoneText.setText("***********");
        }

        if (object.isSex().equals("0"))
        {
            holder.sexImage.setImageResource(R.drawable.man);
        }else if (object.isSex().equals("1"))
        {
            holder.sexImage.setImageResource(R.drawable.women);
        }else {
            holder.sexImage.setImageResource(R.drawable.wemon_man);
        }

//        BitmapUtils bitmapUtils = new BitmapUtils(context);
//        bitmapUtils.configMemoryCacheEnabled(true);
//        bitmapUtils.configDefaultLoadingImage(R.drawable.icon_default);
//        bitmapUtils.configDefaultLoadFailedImage(R.drawable.icon_default);
//        // 加载网络图片
//        bitmapUtils.display(holder.headImage, Cons.BASE_URL+object.getUserimg() );

        Glide.with(context).load(Cons.BASE_URL + object.getUserimg()).placeholder(R.drawable.icon_default).into(holder.headImage);

        return convertView;
    }




    class ViewHolder   {
        TextView nameText;
        TextView phoneText;
        TextView jobText;
        ImageView headImage;
        ImageView sexImage;
    }

}
